/**
 * 历史导航菜单选项卡
 * @type object
 * @since 1.0 <2015-6-19> SoChishun Added.
 */
var SuNavTab = {
    newInstance: function () {
        var obj = {
            config: {
                startup: 0,
                db_name: 'nav_tab',
                container_id: '',
                max_count: 10,
            },
            /**
             * 设置配置选项
             * @param object option
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            set_config: function (option) {
                $.extend(this.config, option);
            },
            /**
             * 生成tab
             * @param Array menus
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            build_tab: function (menus) {
                if (!menus) {
                    return;
                }
                var txt = window.localStorage.getItem(this.config.db_name);
                var otxt;
                if (!txt) {
                    // 如果无数据则生成默认起始页
                    otxt = {items: [this.config.startup], current: this.config.startup};
                } else {
                    var otxt = JSON.parse(txt);
                }
                var $container = $(this.config.container_id);
                $(menus).each(function () {
                    $(this.children).each(function () {
                        $(this.children).each(function () {
                            if ($.inArray(Number(this.id), otxt.items) > -1) {
                                $container.append('<li' + (this.id == otxt.current ? ' class="cur"' : '') + '><a href="' + this.url + '" data-id="' + this.id + '">' + this.name + '</a><i></i></li>');
                            }
                        })
                    })
                })
            },
            /**
             * 新增Tab或激活Tab
             * @param int id
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            add: function (id) {
                if (!id) {
                    return;
                }
                var txt = window.localStorage.getItem(this.config.db_name);
                var otxt;
                if (!txt) {
                    otxt = {items: [id]};
                } else {
                    otxt = JSON.parse(txt);
                    if ($.inArray(id, otxt.items) < 0) {
                        otxt.items.push(id); // 追加到末尾
                        if (otxt.items.length > this.config.max_count) {
                            otxt.shift(); // 如果超过最大数量则删除第一个
                        }
                    }
                }
                alert(JSON.stringify(otxt));
                otxt.current = id;
                window.localStorage.setItem(this.config.db_name, JSON.stringify(otxt));
            },
            /**
             * 删除tab
             * @param int id
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            remove: function (id) {
                var txt = window.localStorage.getItem(this.config.db_name);
                if (!txt) {
                    return;
                }
                var otxt = JSON.parse(txt);
                var idx = $.inArray(id, otxt.items);
                if (idx < 0) {
                    return; // id不存在,直接退出
                }
                if (otxt.items.length < 2) {
                    window.localStorage.removeItem(this.config.db_name); // 如果已空了则删除数据库
                    return;
                }
                if (otxt.items.length > (idx + 1)) {
                    // 如果后面还有tab,则焦点放在后一个
                    otxt.current = otxt.items[idx + 1];
                } else {
                    // 焦点放在前一个
                    otxt.current = otxt.items[idx - 1];
                }
                otxt.items.splice(idx, 1); // 删除已存在的id
                window.localStorage.setItem(this.config.db_name, JSON.stringify(otxt));
            },
            /**
             * 激活标签
             * @param int id
             * @since 1.0 <2015-6-19> SoChishun Added.
             */
            active: function (id) {
                var txt = window.localStorage.getItem(this.config.db_name);
                if (!txt) {
                    return;
                }
                var otxt = JSON.parse(txt);
                otxt.current = id;
                window.localStorage.setItem(this.config.db_name, JSON.stringify(otxt));
            }
        };
        return obj;
    }
}